import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author tmh
 * @date 2024/9/27 22:20
 * @description
 */
public class T17电话号码的字母组合 {
    String[] mapping = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    StringBuilder builder = new StringBuilder("");
    List<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() <= 0) {
            return Collections.emptyList();
        }
        dfs(digits,0);
        return result;
    }

    private void dfs(String str, int num) {
        //中止条件是字符串的长度等于num
        if (str.length() == num) {
            result.add(builder.toString());
            return;
        }
        //挨个每个数字对应的字符串
        String letter = mapping[str.charAt(num) - '0'];
        //遍历字符串中的字符
        for (Character c : letter.toCharArray()) {
            builder.append(c);
            dfs(str, num + 1);
            builder.deleteCharAt(builder.length() - 1);
        }
    }
}
